package com.dd.ent.car.ui.detail;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.dd.ent.car.AppContext;
import com.dd.ent.car.common.bean.base.PageBean;
import com.dd.ent.car.common.bean.base.ResultBean;
import com.dd.ent.car.common.bean.comment.Comment;
import com.dd.ent.car.common.bean.video.Video;
import com.dd.ent.car.common.config.UIConfig;
import com.dd.ent.car.service.comment.CommentService;
import com.dd.ent.car.service.home.choice.ChoiceService;
import com.dd.ent.car.ui.adapter.base.BaseListAdapter;
import com.google.gson.reflect.TypeToken;
import com.pp.ent.Cherry;
import com.pp.ent.base.log.MLog;
import com.pp.ent.core.manage.UIHandler;
import com.pp.ent.view.annotation.Inject;

import java.lang.reflect.Type;

/**
 * Created by dengqu on 2016/8/29.
 */
public class DetailFragmentImp extends DetailFragment<Comment>{
    private final static String TAG = "DetailFragmentImp";

    public final static int VIDEO_HEADER = 0;
    public final static int PIECE_HEADER = 1;
    public final static int FULL_TEXT_HEADER = 2;
    public final static int INTRIGUE_HEADER = 3;
    public final static String HEADER_VIEW_TYPE = "header_view_type";
    public final static String VIDEO = "video";
    private int headerType = VIDEO_HEADER;
    private Video video;
    @Inject
    private CommentService commentService;

    private DetailCommentView.OnCommentItemListener onCommentItemListener;

    @Override
    protected BaseListAdapter<Comment> getListAdapter() {
        return new CommentAdapter(this,onCommentItemListener);
    }

    @Override
    protected void initData() {
        super.initData();
        requestData();
    }

    @Override
    protected void initBundle(Bundle bundle) {
        super.initBundle(bundle);
        headerType = bundle.getInt(HEADER_VIEW_TYPE, VIDEO_HEADER);
        video = (Video) bundle.getSerializable(DetailFragmentImp.VIDEO);
    }

    @Override
    protected void requestData() {
        super.requestData();
        commentService.queryVideoCommentById(video.getUuid());
    }


    @Override
    protected Type getType() {
        return new TypeToken<ResultBean<PageBean<Comment>>>() {
        }.getType();
    }

    @Override
    protected void showRefreshPromptView(ResultBean<PageBean<Comment>> resultBean) {

    }

    @UIHandler(UIConfig.QUERY_COMMENT_BY_VIDEO_ID_SUCCESS)
    public void getListChocieRes(String json) {
        ResultBean<PageBean<Comment>> resultBean = AppContext.createGson().fromJson(json, getType());
        setListData(resultBean);
        onRequestFinish();

    }

    @UIHandler(UIConfig.ADD_COMMENT_TO_VIDEO_SUCCESS)
    public void sendCommentSuccessRes() {
        Toast.makeText(getContext(), "评论成功", Toast.LENGTH_LONG).show();
        mIsRefresh = true;
        requestData();
        if (video != null) {
            video.setCommentCount(video.getCommentCount() + 1);
            Cherry.notifyUI(UIConfig.NOTIFY_VIDEO_UPDATE_COMMENT_COUNT, video);
        }
    }

    @Override
    protected View getHeaderLayout() {
        return null;
    }

    public void setOnCommentItemListener(DetailCommentView.OnCommentItemListener onCommentItemListener) {
        this.onCommentItemListener = onCommentItemListener;
    }
}
